package by.alex.dzeshko.facebooksuperclient.models;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import by.alex.dzeshko.facebooksuperclient.ContextHolder;
import by.alex.dzeshko.facebooksuperclient.R;
import by.alex.dzeshko.facebooksuperclient.models.common.JSONModel;

public class FbNewsFeed extends JSONModel {
	
	private final static String LOG_TAG = "FbNewsFeed";
	
	private final static int NO_DATA = -1;
	
	private final static int ID = R.string.id;
	
	private final static int MESSAGE = R.string.message;

    public static final int FROM = R.string.from;
     
    public static final int NAME = R.string._name;
     
    public static final int TYPE = R.string.type;

    public static final int CREATED_TIME = R.string.created_time;
     
    public static final int UPDATED_TIME = R.string.updated_time;
     
    public static final int LIKES = R.string._likes;

    public static final int COMMENTS = R.string._comments;
     
    public static final int COUNT = R.string.count;
     
    public static final int STORY = R.string.count;

    public static final int CAPTION = R.string.caption;

    public static final int PICTURE = R.string.picture;
	
	private static final int LIKE = R.string.like;

	private static final int LINK = R.string.link;

	private static final int COMMENT = R.string.comment;

	private static final int ACTIONS = R.string.actions;
	
	Context mContext = ContextHolder.getInstance().getContext();
	
	public FbNewsFeed() {
		super();
	}

	public FbNewsFeed(JSONObject jsonObject) {
		super(jsonObject);
	}

	public FbNewsFeed(String jsonString) {
		super(jsonString);
	}

	public String getId() {
		String id = getString(mContext.getString(ID));
		id = id.substring(id.indexOf("_") + 1);
		return id;
	}

	public String getFromId() {
		return getSecondLevelString(mContext.getString(FROM), mContext.getString(ID));
	}

	public String getFromName() {
		return getSecondLevelString(mContext.getString(FROM), mContext.getString(NAME));
	}
	public String getMessage() {
		return getString(MESSAGE);
	}

	public String getType() {
		return getString(TYPE);
	}

	public String getCreatedTime() {
		return getString(CREATED_TIME);
	}

	public int getCommentLink() {
		if(!jo.has(mContext.getString(ACTIONS)))
			return NO_DATA;
		try {
			JSONArray ja = jo.getJSONArray(mContext.getString(ACTIONS));
			for(int i = 0; i<ja.length();i++){
				if(ja.getJSONObject(i).getString(mContext.getString(NAME)).equals(mContext.getString(COMMENT))){
					return 1;
				}
			}
		} catch (JSONException e) {
			Log.e(LOG_TAG, e.toString());
			
		} return NO_DATA;
		
	}

	public int getLikeLink() {
		if(!jo.has(mContext.getString(ACTIONS)))
			return NO_DATA;
		try {
			JSONArray ja = jo.getJSONArray(mContext.getString(ACTIONS));
			for(int i = 0; i<ja.length();i++){
				if(ja.getJSONObject(i).getString(mContext.getString(NAME)).equals(mContext.getString(LIKE))){
					return 1;
				}
			}
		} catch (JSONException e) {
			Log.e(LOG_TAG, e.toString());
		}
		return NO_DATA;
	}
	
	public int getLikesCount() {
		return getSecondLevelInt(mContext.getString(LIKES), mContext.getString(COUNT));
	}
	
	public int getCommentsCount() {
		return getSecondLevelInt(mContext.getString(COMMENTS), mContext.getString(COUNT));
	}
	
	public String getStory() {
		return getString(STORY);
	}
	
	public String getCaption() {
		return getString(CAPTION);
	}
	
	public String getPicture() {
		return getString(PICTURE);
	}
}
